<?php
namespace app\api\controller;

use app\BaseController;

class Uploads extends BaseController
{
    public function files(){
        $key = request()->param('key', '1');
        $folder = request()->param('folder', 'index');
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 上传到本地服务器
//        $savename = \think\facade\Filesystem::disk('public')->putFile( 'uploads', $file);
        $savename = \think\facade\Filesystem::disk('uploads')->putFileAs( 'uploads/' . $folder, $file, (time() + 1) . '.png');
        if ($savename) {

            // 上传成功，返回图片路径
            $path = \app\model\Base::getLocalUrl() . '/' . $savename;//  \app\model\Base::$uploadImageUrl . $savename;
            $savename = '/' . $savename;
//            return json(['code' => 0, 'msg' => '上传成功', 'data' => $path]);
            return showSuccess([
                'key'=> $key,
                'val'=> $savename,
                'src'=> $path
            ], '上传成功');
        } else {
            // 上传失败，返回错误信息
            return json(['code' => 1, 'msg' => $file->getError()]);
        }
    }

    public function byId(){
        $folder = request()->param('folder', 'index');
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');//dd($file);
        $name = (time() + mt_rand(1000, 9999));
        // 上传到本地服务器
//        $savename = \think\facade\Filesystem::disk('public')->putFile( 'uploads', $file);
        $savename = \think\facade\Filesystem::disk('uploads')->putFileAs( 'uploads/' . $folder, $file, $name . '.png');

        if ($savename) {
            // 上传成功，返回图片路径
            $path = \app\model\Base::getLocalUrl() . '/' . $savename;//  \app\model\Base::$uploadImageUrl . $savename;
            $savename = '/' . $savename;
//            return json(['code' => 0, 'msg' => '上传成功', 'data' => $path]);
            return showSuccess([
//                'key'=> $key,
                'val'=> $savename,
                'src'=> $path
            ], '上传成功');
        } else {
            // 上传失败，返回错误信息
            return json(['code' => 1, 'msg' => $file->getError()]);
        }
    }

//    public function file(){
////        $key = request()->param('key');
//        $folder = 'product';//'merchant/video';//request()->param('folder', 'index');
//        // 获取表单上传文件 例如上传了001.jpg
//        $file = request()->file('file');
//        // 上传到本地服务器
////        $savename = \think\facade\Filesystem::disk('public')->putFile( 'uploads', $file);
////        Filesystem
//        $savename = \think\facade\Filesystem::disk('uploads')->putFileAs( 'uploads/' . $folder, $file, (time() + 1) . '.mp4');
//        if ($savename) {
//
//            // 上传成功，返回图片路径
//            $path = \app\model\Base::$uploadImageUrl . $savename;
//            $savename = '/' . $savename;
////            return json(['code' => 0, 'msg' => '上传成功', 'data' => $path]);
//            return showSuccess([
////                'key'=> $key,
//                'val'=> $savename,
//                'src'=> $path
//            ], '上传成功');
//        } else {
//            // 上传失败，返回错误信息
//            return json(['code' => 1, 'msg' => $file->getError()]);
//        }
//    }
}